package com.mall.service.authority.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsResourceCategory;
import com.mall.service.authority.service.ResourceCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-03-11 19:10
 * @Desc 后台资源Controller
 */
@RestController
@RequestMapping("resourceCategory")
@Slf4j
public class ResourceCategoryController {
    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /**
     * 添加后台资源分类
     *
     * @param resourceCategory
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("create")
    public CommonResult<String> create(@RequestBody UmsResourceCategory resourceCategory) {
        log.info("resourceCategory = {}", resourceCategory.toString());
        Boolean flag = resourceCategoryService.checkAndSave(resourceCategory);
        if (flag) {
            return CommonResult.success(null, "后台资源分类添加成功");
        } else {
            return CommonResult.error("后台资源分类名重复");
        }
    }

    /**
     * 删除后台资源分类
     *
     * @param id
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("delete/{id}")
    public CommonResult<String> delete(@PathVariable Long id) {
        log.info("id为{}" + id);
        resourceCategoryService.removeById(id);
        return CommonResult.success(null, "删除后台资源分类成功");
    }

    /**
     * 获取全部后台资源分类
     *
     * @param
     * @return com.mall.commons.common.CommonResult<java.util.List>
     * @Author Pan Rundong
     */
    @GetMapping("listAll")
    public CommonResult<List> getAll() {
        log.info("获取全部后台资源分类");
        List<UmsResourceCategory> list = resourceCategoryService.list();
        return CommonResult.success(list, "已查询到全部后台分类资源");
    }

    /**
     * 修改后台资源分类
     *
     * @param
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("update/{id}")
    public CommonResult<String> update(@PathVariable Long id, @RequestBody UmsResourceCategory umsResourceCategory) {
        log.info("接收后台资源为：" + umsResourceCategory.toString());
        Boolean flag = resourceCategoryService.checkAndUpdate(id, umsResourceCategory);
        if (flag) {
            return CommonResult.success(null, "后台资源分类修改成功");
        } else {
            return CommonResult.error("后台资源分类修改失败");
        }
    }

}
